package cn.itcast.bos.web.action.take_delivery;

import java.util.HashMap;
import java.util.Map;

import org.apache.log4j.Logger;
import org.apache.struts2.ServletActionContext;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Controller;

import com.opensymphony.xwork2.ActionContext;

import cn.itcast.bos.domain.take_delivery.WayBill;
import cn.itcast.bos.service.take_delivery.WayBillService;
import cn.itcast.bos.web.action.common.BaseAction;

@SuppressWarnings("serial")
@Namespace("/")
@ParentPackage("json-default")
@Controller
@Scope("prototype")
public class WayBillAction extends BaseAction<WayBill>{
	
	@Autowired
	private WayBillService wayBillService;
	//用日志保存信息
	private static Logger logger = Logger.getLogger(WayBill.class);
	
	/**
	 * 后台界面快速录入和手动运单的保存，此处用到了日志，如下面注释，日志具体显示了执行的行号
	 * @return
	 */
	@Action(value="savewaybill_quick",results={@Result(name="success",type="json")})
	public String saveWayBill(){
		Map<String,Object> map = new HashMap<>();
		try {
			if(model.getOrder() != null && model.getOrder().getId() == null || model.getOrder().getId()==0){
				model.setOrder(null);
			}
			wayBillService.saveWayBill(model);
			map.put("success", true);
			map.put("msg", "数据已保存");
			logger.info("运单保存成功！"); //13:22:33,348  INFO WayBill:43 - 运单保存成功！
		} catch (Exception e) {
			map.put("success", false);
			map.put("msg", "数据保存失败");
			logger.info("运单保存失败"+e.getMessage());
		}
		ServletActionContext.getContext().getValueStack().push(map);
		return SUCCESS;
	}
	
	/**
	 * 运单的分页显示功能
	 * @author 2017/8/6 添加索引查询功能
	 * @return
	 */
	@Action(value="wayBill_findByPage",results={@Result(name="success",type="json")})
	public String findByPage(){
		Pageable pages = new PageRequest(page - 1, rows, new Sort("id"));
		Page<WayBill> pageData = wayBillService.findByPage(model,pages);
		pushPageDataToValueStack(pageData);
		return SUCCESS;
	}

	/**
	 * 填写运单回显数据
	 */
	@Action(value="findByWayBillNum",results={@Result(name="success",type="json")})
	public String findByWayBillNum(){
		Map<String, Object> map = new HashMap<>();
		WayBill wayBill = wayBillService.findByWayBillNum(model.getWayBillNum());
		if(wayBill != null){
			map.put("msg", true);
			map.put("wayBillData", wayBill);
			logger.info("运单回显成功");
		}else{
			map.put("msg", false);
			logger.info("运单回显失败");
		}
		ActionContext.getContext().getValueStack().push(map);
		return SUCCESS;
	}
}
